<?php

if (!defined('access')){
	die ('Direct access Denied!');
} else {

	define('libuser',true);

	function getBan($userid){
		$data = execQuery("SELECT userban FROM membres WHERE userid = '". $userid ."'")->fetch();
		return $data['userban'];
	}

	function Login($username,$userpass){
		$data = execQuery("SELECT userid FROM membres WHERE username = '".$username."' AND userpass = '". md5($userpass) ."'")->fetch();
		$userid = $data['userid'];
		if (!empty($userid)){
			if (getBan($userid) == 0){
				execQuery("UPDATE membres SET userlogin = '". date('Y-m-d H:i:s') ."' WHERE userid = '". $userid ."'");
				return $_SESSION['userid'] = $userid;
			} else {
				echo 'Désolé, mais vous avez été banni !';
			}
		} else {
			echo "Désolé, mais cet utilisateur n'existe pas...";
		}
	}

	function CreateUser($username,$userpass,$usermail,$userbirthday){
		$name = execQuery("SELECT username FROM membres WHERE username = '". $username ."'")->fetch();
		$mail = execQuery("SELECT usermail FROM membres WHERE usermail = '". $usermail ."'")->fetch();
		if (!empty($name['username'])){
			echo 'Désolé, mais ce nom d\'utilisateur est déjà pris...';
		} elseif (!empty($mail['usermail'])){
			echo 'Désolé, mais vous possédez déjà un compte sur ce site !';
		} else {
			execQuery("INSERT INTO membres (username,userpass,usermail,userbirthday) VALUES
           		 	   ('". $username ."','". md5($userpass) ."','". $usermail ."','". $userbirthday ."')");
				 
			echo 'Félicitation, vous êtes maintenant inscrit, vous pouvez vous connecter !';
		}
	}

	class User {
		
		public function __construct($userid){
			$data = execQuery("SELECT username,usermail,userbirthday,usercreate FROM membres WHERE userid = '". $userid ."'")->fetch();
			$this->username = $data['username'];
			$this->usermail = $data['usermail'];
			$this->userbirthday = $data['userbirthday'];
			$this->usercreate = $data['usercreate'];		
		}
	
		public function getName(){
			return $this->username;
		}
		
		public function getMail(){
			return $this->usermail;
		}
		
		public function getBirthday(){
			return $this->userbirthday;
		}
		
		public function getCreate(){
			return $this->usercreate;
		}
		
	}
}	
?>